<?php
namespace app\api\controller\yluser;
use x\my\Captcha;
class User extends Base
{

    //小程序, 获取微信openid
    public function getwxopenid(){
        $d = $this->post;
        $rule = [
            'code'=>'require',
        ];
        $msg = [];
        check($rule,$msg,$d);
        $url = 'https://api.weixin.qq.com/sns/jscode2session?appid='.c('api.wx_xcx_appid').'&secret='.c('api.wx_xcx_appsecret').'&js_code=' . $d['code'] . '&grant_type=authorization_code';
        //yourAppid为开发者appid.appSecret为开发者的appsecret,都可以从微信公众平台获取；
        $info = file_get_contents($url);//发送HTTPs请求并获取返回的数据，推荐使用curl
        $json = json_decode($info);//对json数据解码
        $arr = get_object_vars($json);
        if(isset($arr['openid'])){
          $openid = $arr['openid'];
          return suc(['openid'=>$openid]);
        }else{
          err('获取微信信息失败');
        }
    }

    //绑定 - 小程序
    public function bind(){
        $d = $this->post;
        //判断短信验证码
        $rule = [
            'phone'=>'require',
            'openid' => 'require',
            'code'=>'require',
        ];
        $msg = [
            'phone.require' => '请输入手机号',
            'openid.require' => '请输入openid',
            'code.require' => '请输入验证码',
        ];
        check($rule,$msg,$d);
        //短信验证
        if(!m('sms','xcom')->checkCode($d['phone'], 'bind', $d['code'])){
            err(m('sms','xcom')->getError());
        }

        $user_id = s('User','c')->bind($d['phone'],"",$d['openid'],$d['invitecode']);
        if ($user_id) {
            $rs = m('User','c')->autoLogin($user_id);
            $rs['level_name'] = c('type.user_level')[$rs['level']];
            $rs['sex_name'] = c('type.sex')[$rs['sex']];
            $rs['status_name'] = c('type.status')[$rs['status']];
            $rs['create_time_name'] = com_time_format($rs['create_time']);
            $rs['invitecode'] = en_id($rs['id']);
            $rs['relation_invitecode'] = en_id($rs['relation_id']);
            suc($rs);
        }else{
            err(s('User','c')->getError());
        }
    }

    //注册 - app
    public function register(){
        $d = $this->post;
        //判断短信验证码
        $rule = [
            'phone'=>'require',
            'password'=>'require',
            'code'=>'require',
        ];
        $msg = [
            'phone.require' => '请输入手机号',
            'password.require' => '请输入密码',
            'code.require' => '请输入验证码',
        ];
        check($rule,$msg,$d);
        //短信验证
        if(!m('sms','xcom')->checkCode(trim($d['phone']), 'register', trim($d['code']))){
            err(m('sms','xcom')->getError());
        }
        $password = decode($d['password']);
        $user_id = s('User','c')->register($d['phone'],$password,$d['invitecode']);
        if ($user_id) {
            suc('注册成功');
        }else{
            err(s('User','c')->getError());
        }
    }

    //token获取userinfo
    public function gettokenuserinfo(){
        $d = $this->post;
        $rule = [
            'token'=>'require',
        ];
        $msg = [
            'token.require' => '请输入token',
        ];
        check($rule,$msg,$d);

        $field = tokenType();
        $rs = $this->db->query("select * from user where {$field}=?",[$d['token']],1);
        if ($rs) {
            $rs['level_name'] = c('type.user_level')[$rs['level']];
            $rs['sex_name'] = c('type.sex')[$rs['sex']];
            $rs['status_name'] = c('type.status')[$rs['status']];
            $rs['create_time_name'] = com_time_format($rs['create_time']);
            $rs['invitecode'] = en_id($rs['id']);
            $rs['relation_invitecode'] = en_id($rs['relation_id']);
            suc($rs);
        }else{
            err('该token不存在');
        }
    }
    //openid获取token
    public function getopeniduserinfo(){
        $d = $this->post;
        $rule = [
            'openid'=>'require',
        ];
        $msg = [
            'openid.require' => '请输入openid',
        ];
        check($rule,$msg,$d);

        $rs = $this->db->query("select * from user where openid=?",[$d['openid']],1);
        if ($rs) {
            $rs = m('User','c')->autoLogin($rs['id']);
            $rs['level_name'] = c('type.user_level')[$rs['level']];
            $rs['sex_name'] = c('type.sex')[$rs['sex']];
            $rs['status_name'] = c('type.status')[$rs['status']];
            $rs['create_time_name'] = com_time_format($rs['create_time']);
            $rs['invitecode'] = en_id($rs['id']);
            $rs['relation_invitecode'] = en_id($rs['relation_id']);
            suc($rs);
        }else{
            err('该openid不存在');
        }
    }

	//用户登陆 - app，h5
    public function login(){
        $d = $this->post;
        $rule = [
            'phone'=>'require',
            'password'=>'require',
        ];
        $msg = [
            'phone.require' => '请输入手机号',
            'password.require' => '请输入密码',
        ];
        check($rule,$msg,$d);

        $password = decode($d['password']);
        $token = m('User','c')->login($d['phone'],$password);
        if($token){
            $tokenType = tokenType();
            $rs = $this->db->query("select * from user where {$tokenType}=?",[$token],1);
            $rs['level_name'] = c('type.user_level')[$rs['level']];
            $rs['sex_name'] = c('type.sex')[$rs['sex']];
            $rs['status_name'] = c('type.status')[$rs['status']];
            $rs['create_time_name'] = com_time_format($rs['create_time']);
            $rs['invitecode'] = en_id($rs['id']);
            $rs['relation_invitecode'] = en_id($rs['relation_id']);
            suc($rs);
        }else{
            err(m('User','c')->getError());
        }
    }

    //忘记密码
    public function forget(){
        $d = $this->post;
        $rule = [
            'phone'=>'require',
            'password'=>'require',
            'code'=>'require',
        ];
        $msg = [
            'phone.require' => '请输入手机号',
            'password.require' => '请输入新密码',
            'code.require' => '请输入验证码',
        ];
        check($rule,$msg,$d);
        //短信验证
        if(!m('sms','xcom')->checkCode($d['phone'], 'forget', $d['code'])){
            err(m('sms','xcom')->getError());
        }
        $password = decode($d['password']);
        $rs = m('User','c')->forget($d['phone'],$password);
        if($rs){
            suc();
        }else{
            err(m('User','c')->getError());
        }
    }

    //退出 - app
    public function logout(){
        $d = $this->post;
        $rule = [
            'token'=>'require',
        ];
        $msg = [
            'token.require' => '请输入token',
        ];
        check($rule,$msg,$d);
        $this->db->update('user',['app_token'=>''],['app_token=?',[$d['token']]]);
        suc();
    }

    public function smsLogin(){
        $d = $this->post;
        $rule = [
            'phone'=>'require',
            'code'=>'require',
        ];
        $msg = [
            'phone.require' => '请输入手机号',
            'code.require' => '请输入验证码',
        ];
        check($rule,$msg,$d);
        //短信验证
        if(!m('sms','xcom')->checkCode($d['phone'], 'smslogin', $d['code'])){
            err(m('sms','xcom')->getError());
        }
        $user = $this->db->query("select * from user where phone=?",[$d['phone']],1);
        if($user){
            $rs = m('User','c')->autoLogin($user['id']);
            $rs['level_name'] = c('type.user_level')[$rs['level']];
            $rs['sex_name'] = c('type.sex')[$rs['sex']];
            $rs['status_name'] = c('type.status')[$rs['status']];
            $rs['create_time_name'] = com_time_format($rs['create_time']);
            $rs['invitecode'] = en_id($rs['id']);
            $rs['relation_invitecode'] = en_id($rs['relation_id']);
            suc($rs);
        }else{
            err("用户不存在");
        }
    }
    //用户支付密码
    public function editSPwd(){
        $this->checkLogin();
        $d = $this->post;
        $rule = [
            'spassword'=>'require|min:6',
            'phone'=>'require',
            'code'=>'require',
        ];
        $msg = [
            'spassword.require' => '请输入支付密码',
            'spassword.minlenght' => '支付密码至少为六位数',
            'phone.require' => '请输入电话',
            'code.require' => '请输入安证码',
        ];
        check($rule,$msg,$d);
        //短信验证
        if(!m('sms','xcom')->checkCode($d["phone"], 'remake', $d['code'])){
            err(m('sms','xcom')->getError());
        }
        $password = decode($d['spassword']);
        $data = [
            "s_password" => new_com_password($password,$this->userinfo["salt"]),
            "update_time" => time()
        ];
        $re = $this->db->update("user",$data,["id=?",[$this->userinfo["id"]]]);
        if($re){
            return suc('保存成功');
        }else{
            err('保存错误!');
        }
    }

}
